import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/5/27
 * Time: 22:24
 */

public class MinStack {
    //辅助栈
    /*private final Stack<Integer> stack;
    private final Stack<Integer> minStack;
    public MinStack() {
        this.stack = new Stack<>();
        this.minStack = new Stack<>();
    }
    public void push(int val) {
        stack.push(val);
        //minStack栈顶压入最小值
        if(minStack.empty() || val <= minStack.peek()){
            minStack.push(val);
        }
    }

    public void pop() {
        if(!stack.empty()){
            int ret = stack.pop();
            //如果最小值是stack的栈顶元素,minStack的栈顶元素也应该pop
            if(ret == minStack.peek()){
                minStack.pop();
            }
        }
    }

    public int top() {
        if(stack.empty()){
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        if(stack.empty()){
            return -1;
        }
        return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        minStack.getMin();
    }*/
    private static class Node{
        public int val;
        public int min;
        public Node(int val, int min){
            this.val = val;
            this.min = min;
        }
    }
    private final Stack<Node> stack;
    public MinStack(){
        this.stack = new Stack<>();
    }
    public void push(int val){
        if(stack.isEmpty()){
            stack.push(new Node(val, val));
        }else {
            stack.push(new Node(val, Math.min(val, stack.peek().min)));
        }
    }
    public void pop(){
        if(stack.isEmpty()){
            return;
        }
        stack.pop();
    }
    public int top(){
        if(stack.isEmpty()){
            return -1;
        }
        return stack.peek().val;
    }
    public int getMin(){
        if(stack.isEmpty()){
            return -1;
        }
        return stack.peek().min;
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        minStack.getMin();
    }
}